package cc.rengu.igas.momp.common.dao.impl;

import cc.rengu.igas.momp.common.dao.TMMchntMonthDepositMapper;
import cc.rengu.igas.momp.common.entity.TMMchntMonthDeposit;
import cc.rengu.igas.momp.common.util.MompDateUtil;
import cc.rengu.jradp.mods.dbs.Database;
import cc.rengu.oltp.service.common.constant.AppConfigConstant;
import cc.rengu.oltp.service.common.constant.AppParamConstant;
import cc.rengu.oltp.utility.util.DbsUtil;
import cc.rengu.oltp.utility.util.XmlConfigUtil;
import cc.rengu.oltp.utility.util.XmlTreeUtil;
import cc.rengu.utility.cache.UnifiedCache;
import cc.rengu.utility.log.RgLog;
import cc.rengu.utility.log.RgLogger;

/**
 * 商户月日均存款接口实现
 *
 * @author liujinan
 * @version 1.0.0
 * @date 2020/4/29
 * @description 商户月日均存款接口实现
 */
public class TMMchntMonthDepositMapperImpl implements TMMchntMonthDepositMapper {

    private RgLogger rglog = RgLog.getLogger(this.getClass().getName());
    String dbPoolName = XmlConfigUtil.getXmlConfigCacheKeyValue(AppParamConstant.DATABASE_CONF_NODE, AppConfigConstant.MOMP_POOL_NAME);

    /**
     * 获取商户上月日均存款数据库操作接口实现
     *
     * @param instId  法人机构号
     * @param mchntNo 商户编号
     * @param month   月份
     * @return cc.rengu.igas.momp.common.entity.TMMchntMonthDeposit
     * @throws Exception 异常信息
     * @author Jinan Liu
     * @since 2020/6/10 15:21
     */
    @Override
    public TMMchntMonthDeposit selectLastMonthDepositInfo(String instId, String mchntNo, String month) throws Exception {

        XmlTreeUtil xmlTreeUtil = new XmlTreeUtil();
        String corporation = xmlTreeUtil.getXmlTreeStringValue(AppParamConstant.CORPORATION);
        if (null != corporation && !corporation.isEmpty()) {
            String dbPoolCashNode = corporation.trim() + AppParamConstant.DATABASE_CONF_NODE;
            dbPoolName = XmlConfigUtil.getXmlConfigCacheKeyValue(dbPoolCashNode, AppConfigConstant.MOMP_POOL_NAME);
        }
        rglog.error("DBPoolName=[{}]", dbPoolName);

        DbsUtil dbsUtil = new DbsUtil(dbPoolName);
        String sqlStr = (String) UnifiedCache.get(AppParamConstant.SQL_MAPPER_CACHE, "selectLastMonthDepositInfo");

        TMMchntMonthDeposit tmMchntMonthDeposit = new TMMchntMonthDeposit();
        tmMchntMonthDeposit.setInstId(instId);
        tmMchntMonthDeposit.setMchntNo(mchntNo);
        tmMchntMonthDeposit.setMchntMonth(month);

        int iReturnCode = dbsUtil.dbsExecuteSql(sqlStr, tmMchntMonthDeposit, tmMchntMonthDeposit);
        if (Database.DBS_SUCCESS != iReturnCode) {
            rglog.error("获取法人机构号<{}>的商户编号<{}>的月份<{}>的商户上月日均存款失败!<{}>", instId, mchntNo, month, iReturnCode);
            return null;
        }

        return tmMchntMonthDeposit;
    }


    /**
     * 使用商户号查询商户月日均存款
     *
     * @param tmMchntMonthDeposit
     * @throws Exception 异常信息
     * @author liujinan
     * @date 2020/4/29
     */
    @Override
    public TMMchntMonthDeposit selectTMMchntMonthDepositByMchntNo(String instId, String mchntNo) throws Exception {

        XmlTreeUtil xmlTreeUtil = new XmlTreeUtil();
        String corporation = xmlTreeUtil.getXmlTreeStringValue(AppParamConstant.CORPORATION);
        if (null != corporation && !corporation.isEmpty()) {
            String dbPoolCashNode = corporation.trim() + AppParamConstant.DATABASE_CONF_NODE;
            dbPoolName = XmlConfigUtil.getXmlConfigCacheKeyValue(dbPoolCashNode, AppConfigConstant.MOMP_POOL_NAME);
        }
        rglog.error("DBPoolName=[{}]", dbPoolName);

        DbsUtil dbsUtil = new DbsUtil(dbPoolName);
        String sqlStr = (String) UnifiedCache.get(AppParamConstant.SQL_MAPPER_CACHE, "mompSelectTMMchntMonthDepositByMchntNo");
        //获得当前月的前一个一个月
        String month = MompDateUtil.getEveryMonth(-1);

        // 按接口赋查询条件
        TMMchntMonthDeposit tmMchntMonthDeposit = new TMMchntMonthDeposit();
        tmMchntMonthDeposit.setInstId(instId);
        tmMchntMonthDeposit.setMchntNo(mchntNo);
        tmMchntMonthDeposit.setMchntMonth(month);

        int iReturnCode = dbsUtil.dbsExecuteSql(sqlStr, tmMchntMonthDeposit, tmMchntMonthDeposit);
        if (Database.DBS_SUCCESS != iReturnCode) {
            rglog.error("法人机构号<{}>商户号<{}>月份<{}> 获取月日均存款失败iReturnCode=<{}>！", instId, mchntNo, month, iReturnCode);
            return null;
        }
        return tmMchntMonthDeposit;
    }
}
